#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "c-utils/utils.h"

void print_hash(uint8_t *p)
{
    for (unsigned int i = 0; i < 16; ++i)
    {
        printf("%02x", p[i]);
    }
    printf("\n");
}

int test_md5(int argc, const char *argv[])
{
    const char *raw_ctx = "hello,wolrd!";
    const utils_md5_t md5 = {.value = {0xc2, 0x6c, 0xb2, 0x99, 0xbb, 0x0c, 0x6b, 0xe3, 0x70, 0x92, 0x31, 0xe4, 0xa5, 0xb8, 0xda, 0x6c}};

    int ret = 0;
    utils_md5_t out;

    if (utils_md5_sum(raw_ctx, strlen(raw_ctx), &out))
        return -1;

    print_hash(out.value);
    if (memcmp(&md5, &out, sizeof(utils_md5_t)) != 0)
        return -1;

    printf("compare: equal\n");
    const char *test_file_name = "md5_test_file";
    if (utils_file_write(test_file_name, 0, raw_ctx, strlen(raw_ctx)) < 0)
        return -1;

    if (utils_md5_file(test_file_name, &out))
        return -1;

    print_hash(out.value);
    if (memcmp(&md5, &out, sizeof(utils_md5_t)) != 0)
        return -1;
    remove(test_file_name);
    return 0;
}